#include "main.h"
#define M_PI 3.14159


void Planet::setTextures()
{
	path[0]="textures/mercury.bmp";
	path[1]="textures/venus.bmp";
	path[2]="textures/earth.bmp";
	path[3]="textures/mars.bmp";
	path[4]="textures/jupiter.bmp";
	path[5]="textures/saturn.bmp";
	path[6]="textures/uranus.bmp";
	path[7]="textures/neptune.bmp";
}

Planet::Planet(void)
{
}

Planet::Planet(double x, double y, double z,double r,float speed,int nr)
{
	//Set new coordonates for the translation Matrix
	_x=x;
	_y=y;
	_z=z;
	//Set the planet radius
	_radius=r;
	//Set the speed for orbiting around sun
	_speed=speed;
	//Set the texture number
	texNr=nr;
	//Place the planet on a radom place on the orbit
	spin=rand()%360;
	//Initialize the textures
	setTextures();
	//nrMoons=-1;
}

Planet::~Planet(void)
{
}

void Planet::init()
{
  //Initialize the texture for the planet
  int i=0;
  //Load identity matrix
  glLoadIdentity();
  glOrtho(-1.0, 1.0, -1.0, 1.0, -2.0, 2.0);
     
  planet = gluNewQuadric();
  gluQuadricTexture( planet, GL_TRUE);
  planetTexture = LoadBitmap(path[texNr].c_str());
  //Initialize textures for the moons
  for(i=0;i<nrMoons;i++)
	{
	moons[i].init();
	}
}

void Planet::Spin()
{
	int i;
	//Update the speed
	spin +=_speed;
	// Spin for ever
	if (spin > 360.0)
	spin = spin - 360.0;
	//Spin the moons
	for(i=0;i<nrMoons;i++)
	{
	moons[i].spinm();
	}
}

void Planet::draw()
{
	GLfloat ambient[] = {0.1,0.1,0.1,1.0};
	GLfloat no_ambient[] = {0.2,0.2,0.2,1.0};
	//moon counter
	int i=0;
	//Print planet's name
	printTag();
	//Draw orbit
	drawOrbit();
	//increase the darkness level for the hidden part of the planets and the moon
	glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient);
	//Apply textures
	glEnable ( GL_TEXTURE_2D );
	//Select texture to apply(the texture it's initialized in the init function
    glBindTexture ( GL_TEXTURE_2D, planetTexture);
	glMatrixMode(GL_MODELVIEW);
	//Push matris
	glPushMatrix();
	// Multiply the current matrix by a rotation matrix
	glRotated((GLdouble) spin, 0.0, 1.0, 0.0);
	//Rotate planet -90 on x axis, so the emispheres are set corectlly
	glRotated((GLdouble) -90, 1.0, 0.0, 0.0);
	//set location of planet
	glTranslatef (_x, _y, _z);
	//Spin planet around self axis
	glRotated((GLdouble) spin*40, 0.0, 0.0, 1.0);
	// Draw a sphere on the screen
    gluSphere( planet, _radius, 36, 72);
	//Spin planet around self axis
	glRotated((GLdouble) spin*40, 0.0, 0.0, -1.0);
	//Draw ring for Saturn and Uranus
	if(tag=="Saturn") ring(20,0.2);
	if(tag=="Uranus") ring(5,0.1);
	//Disable textures
	glDisable (GL_TEXTURE_2D);
    for(i=0;i<nrMoons;i++)
	{
	moons[i].draw();
	}
	
	glPopMatrix();
	//return to default ambient
	glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,no_ambient);
}

void Planet::moon(int k, double x, double y,double z,double r,double speedm)
{
	//Set the characteristics for each moon where k is the moon number
	moons[k]=Moon(x,y,z,r,speedm);
}

void Planet::setTag(char *s)
{
	//Set planet name
	tag=s;
}

void Planet::printTag()
{
//Disable lighting conditions
glDisable(GL_LIGHTING);
//Set the color for the text
glColor4f(1.0,1.0,1.0,1);
//Define and calculate the angle of the planet in order to place the text above the planet
float angle;
angle=spin*2*M_PI/360;
int len, i;
//Set the new position for the text
glRasterPos3f(_x*cos(angle),_radius+0.1,-_x*sin(angle));
//Get tag length
len = strlen(tag);
//Print one letter at a time
for (i = 0; i < len; i++)   glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, tag[i]);
//Disable color
glColor4f(0.0,0.0,0.0,0.0);
//Enable lighting conditions
glEnable(GL_LIGHTING);
}

void Planet::drawOrbit()
{
//Disable lighting conditions
glDisable(GL_LIGHTING);
//Set the color for the text
glColor4f(0.6,0.8,1,1.0);
	double angle=0;
	//Draw Orbit (in this case a circle)
	glBegin(GL_LINE_LOOP);
    for(int i=0;i<360;i++)
	{
        angle = i*2*M_PI/360;
		glVertex3f(_x*cos(angle),0,_x*sin(angle));

    }
    glEnd();
glColor4i(0,0,0,0);
glEnable(GL_LIGHTING);
}

void Planet::updateSpeed(float update)
{
	//Update speed when pressing the 'o' or the 'p' key
	_speed=update;
}

void Planet::ring(float angle,double size)
{
	//Draw a disk around each planet having one
	//Disable lighting
	glDisable(GL_LIGHTING);
	//rotate ring with the -angle value
	glRotated((GLdouble) -angle, 0.0, 1.0, 0.0);
	//set color
	glColor3f(1,0.97,0.86);
	//spin the ring around the planet
	glRotated((GLdouble) spin*40, 0.0, 0.0, 1.0);
	//draw the ring
	gluDisk(planet,(_radius*2),(_radius*2+size),30,30);
	//stop the spin
	glRotated((GLdouble) spin*40, 0.0, 0.0, -1.0);
	//rotate ring with the angle value
	glRotated((GLdouble) angle, 0.0, 1.0, 0.0);
	//enable lighting
	glEnable(GL_LIGHTING);
	
}